package com.iot.common.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {

    /**
     * 获取当前时间戳 秒
     *
     * @return
     */
    public static long getCurrentStamp() {
        return System.currentTimeMillis() / 1000;
    }
    /**
     * 获取当日时间戳 秒
     *
     * @return
     */
    public static long getTodayCurrentStamp() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 将LocalDate转换为Instant，获取当天开始的时间戳（秒）
        Instant startOfDayInstant = currentDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
        long startOfDayTimestampSeconds = startOfDayInstant.getEpochSecond();

        return startOfDayTimestampSeconds;
    }

    public static String getCurrentTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getTimeStringFromTimeStamp(String timeStamp) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long time = Long.parseLong(timeStamp) * 1000;
            return format.format(time);
        } catch (Exception e) {
            return "1970-01-01 00:00:00";
        }
    }

    public static String getTimeStampFromTimeString(String timeString) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(timeString);
            long l = date.getTime() / 1000;
            // String stf =
            times = String.valueOf(l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    public static String getFileTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }

    public static String getDurationTimeStr(long second) {
        String timeStr = "";
        int day = (int) (second / 86400);
        if (day > 0) {
            timeStr += (day + "天");
        }
        int leaveSec = (int) second % 86400;

        int hour = (int) (leaveSec / 3600);
        if (day == 0) {
            if (timeStr.length() > 0) {
                timeStr += (hour + "小时");
            }
        } else {
            timeStr += (hour + "小时");
        }
        leaveSec = leaveSec % 3600;

        int min = (int) ((leaveSec) / 60);
        if (min == 0) {
            if (timeStr.length() > 0) {
                timeStr += (min + "分钟");
            }
        } else {
            timeStr += (min + "分钟");
        }
        leaveSec = leaveSec % 60;

        if (leaveSec == 0) {
            if (timeStr.length() > 0) {
                timeStr += (leaveSec + "秒");
            }
        } else {
            timeStr += (leaveSec + "秒");
        }

        return timeStr;
    }
}
